//
//  SZLoginViewController.m
//  OTIS_PJ
//
//  Created by sunze on 16/4/22.
//  Copyright © 2016年 sunzeboy. All rights reserved.
//

#import "SZLoginViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "UIColor+Extention.h"
#import "HDHomeViewController.h"
@interface SZLoginViewController ()

/**
 登录
 */
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

/**
 登录父控件
 */
@property (weak, nonatomic) IBOutlet UIStackView *loginFatherView;

/**
 登录账户父控件
 */
@property (weak, nonatomic) IBOutlet UIView *userFatherView;

/**
 登录密码父控件
 */
@property (weak, nonatomic) IBOutlet UIView *passwordFatherView;

/**
 版本
 */
@property (weak, nonatomic) IBOutlet UILabel *verson;

@end

@implementation SZLoginViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
    
    if (self.passWordTF) {
        self.passWordTF.text = @"";
    }
 
  
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden=NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];

}


- (BOOL)shouldAutorotate{
    return NO;
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

// 支持屏幕旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return NO;
}
// 画面一开始加载时就是竖向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

#pragma mark - 初始化登录界面
-(void)setUp
{
    //版本
    self.verson.text = [NSString stringWithFormat:@"V%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    //切圆角
    [self.loginBtn.layer setMasksToBounds:YES];
    [self.loginBtn.layer setCornerRadius:5.0];
    
    
    //设置密码父控件的圆角和边框的宽度.颜色
    [self.passwordFatherView.layer setBorderWidth:1.0];
    self.passwordFatherView.layer.borderColor=[UIColor colorWithHexString:@"d2d2d2"].CGColor;
    [self.userFatherView.layer setBorderWidth:1.0];
    self.userFatherView.layer.borderColor=[UIColor colorWithHexString:@"d2d2d2"].CGColor;
    [self.view layoutIfNeeded];
    
    //密码是否可见的图片的设置
    UIImage *imageEYe = [UIImage imageNamed:@"yanjing1"];
    UIImage *imageEYe2 = [UIImage imageNamed:@"yanjing2"];

    CGFloat w = imageEYe.size.width*2/3;
    CGFloat h = imageEYe.size.height*2/3;
    CGFloat x = SCREEN_WIDTH-30-w;
    CGRect rect = [self.passWordTF.leftView convertRect:self.passWordTF.leftView.frame toView:self.view];
    CGFloat y = CGRectGetMinY(rect)-9;
    UIButton *btnEye = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEye.frame = CGRectMake(x, y, w, h);
    [btnEye setImage:imageEYe forState:UIControlStateSelected];
    [btnEye setImage:imageEYe2 forState:UIControlStateNormal];
    [btnEye setAdjustsImageWhenHighlighted:NO];
    
    //密码是否可见的点击事件
    [btnEye addTarget:self action:@selector(blink:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnEye];
    self.passWordTF.secureTextEntry = !btnEye.selected;

}
//密码是否可见的点击事件
-(void)blink:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    self.passWordTF.secureTextEntry = !btn.selected;

}

/**
 登录的点击事件

 @param sender 传入一个Btn
 */
- (IBAction)loginClick:(id)sender {
    HDHomeViewController *home = [[HDHomeViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
}

//点击屏幕让键盘退下去
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self textResignFirstResponder];
}
//让输入框成为第一响应者
-(void)textResignFirstResponder{
    [self.userNameTF resignFirstResponder];
    [self.passWordTF resignFirstResponder];
}

#pragma mark - 登录重置操作


-(NSString*)getNowDateString:(NSDate*)date{
    NSDateFormatter * df2 = [[NSDateFormatter alloc] init];
    [df2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* nowDateStr = [df2 stringFromDate:date];
    return nowDateStr;
    
}



@end
